import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { userInfo } from '@/type/userType'
export const useUserStore = defineStore('user', () => {


  const _userInfo_string = sessionStorage.getItem('userInfo')
  let _userInfo_obj = {
    "nickname": "",
    "role": [],
    "comment": "",
    "gender": undefined,
    "mobile": 0,
    "_id": "",
    "username": ""
  }

  const isLogin = ref(false);

  const menu = ref([])


  if (_userInfo_string) {

    _userInfo_obj = {
      ..._userInfo_obj,
      ...JSON.parse(_userInfo_string)
    }
    isLogin.value = true
  }

  const userInfo = ref<userInfo>(_userInfo_obj)






  const setUserInfo = (val: userInfo) => {

    userInfo.value = val
    isLogin.value = true
  }



  const clearUserInfo = () => {

    userInfo.value = {
      "nickname": "",
      "role": [],
      "comment": "",
      "gender": undefined,
      "mobile": 0,
      "_id": "",
      "username": ""
    }
    menu.value = []
    isLogin.value = false

    sessionStorage.clear()
  }

  function setMenu(val: any) {
    console.log(val);


    menu.value = val
  }

  return { userInfo, setUserInfo, isLogin, clearUserInfo, menu, setMenu }

})
